import 'dart:async';
import 'dart:ffi';
import 'package:flutter/material.dart';

import 'dart:math';
import '../admob/admob_page.dart';
import '../base/MyScaffold.dart';
import '../import/import_file_page.dart';
import '../internationalization/internationalization.dart';
import '../login/GetViewAndGetWidget.dart';
import '../login/Home.dart';
import '../login/google_login.dart';
import '../share/share_page.dart';
import 'package:get/get.dart';

import '../ui/loginregister/Login.dart';

class FeaturesPage extends StatefulWidget {
  const FeaturesPage({Key? key}) : super(key: key);

  @override
  State<FeaturesPage> createState() => _FeaturesPageState();
}

class _FeaturesPageState extends State<FeaturesPage>
    with TickerProviderStateMixin {
  StreamController controller = StreamController();
  late StreamSink sink;
  late StreamSubscription subscription;
  final featureList = [
    FeatureModel("文件导入功能", ImportFilePage(title: "文件导入功能")),
    FeatureModel("文件分享", SharePage()),
    // FeatureModel("PDF阅读器", PDFPage()),
    FeatureModel("广告", AdMobPage()),
    FeatureModel("Google登录", const GoogleLogin()),
    FeatureModel("GetX", const Home()),
    FeatureModel("国际化", const Language()),
    FeatureModel("登录", const Login()),
  ];
  var num = 0;
  var jian = 0;

  /// 长按事件
  Future<int> longPress() async {
    // await Future.delayed(Duration(seconds: 1));
    num++;
    setState(() {});
    List list = ["郝家伟", 10, false];
    for (var o in list) {
      if (o is bool) {
        print("是bool类型 $o");
      } else if (o is String) {
        print("是字符串类型 $o");
      } else if (o is int) {
        print("是数字类型 $o");
      }
    }
    jian--;
    print("自检 $jian");
    return num;
  }

  void _addDataToStream() async {
    int data = await longPress();
    // _streamDemo.add(data);
    sink.add(data);
  }

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    sink = controller.sink;
    sink.add('A');
    sink.add(1);
    sink.add({'a': 1, 'b': 2});
    // subscription = controller.stream.listen((data) {
    //   num = data;
    //   print(num);
    //   setState(() {});
    // });
  }

  void onData(String data) {
    print('---------${data}');
    setState(() {
      // _data = data;
    });
  }

  void onError(error) {
    print('--------error:$error');
  }

  void onDone() {
    print('--------Done');
  }

  @override
  void dispose() {
    // TODO: implement dispose
    super.dispose();
    sink.close();
    controller.close();
    subscription.cancel();
  }

  @override
  Widget build(BuildContext context) {
    return MyScaffold(
        // appBar: AppBar(
        //   centerTitle: true,
        //   title: const Text('Flutter Future Demo'),
        // ),
        body: Column(
      children: [
        Expanded(
          child: Container(
            height: 400,
            child: ListView.separated(
              itemCount: featureList.length,
              separatorBuilder: (contenxt, index) => const Divider(),
              itemBuilder: (BuildContext context, int index) {
                final model = featureList[index];
                return TextButton(
                  onPressed: () {
                    // Navigator.of(context).push(
                    //   MaterialPageRoute(
                    //     builder: (context) => model.child,
                    //   ),
                    // );
                    Get.to(model.child);
                  },
                  child: Row(
                    children: [
                      Text(model.title),
                      const Spacer(),
                      // const Icon(
                      //   Icons.arrow_back,
                      //   color: Colors.blue,
                      //   size: 20,
                      // ),
                      Transform(
                        alignment: Alignment.center,
                        transform: Matrix4.rotationZ(3.15),
                        child: const Icon(
                          Icons.arrow_back_ios_sharp,
                          color: Colors.blue,
                          size: 20,
                        ),
                      ),
                    ],
                  ),
                );
              },
            ),
          ),
        ),
        Row(
          mainAxisAlignment: MainAxisAlignment.spaceAround,
          mainAxisSize: MainAxisSize.max,
          // //交叉轴的布局方式，对于column来说就是水平方向的布局方式
          crossAxisAlignment: CrossAxisAlignment.center,
          //就是字child的垂直布局方向，向上还是向下
          verticalDirection: VerticalDirection.down,
          children: [
            MaterialButton(
              onPressed: () {
                longPress();
              },
              child: Text(
                'MaterialButton',
                style: TextStyle(color: Colors.white),
              ),
              color: Colors.blue,
              height: 45,
              shape: RoundedRectangleBorder(
                side: BorderSide(color: Colors.black),
                borderRadius: BorderRadius.circular(4),
              ),
            ),
            ClipRRect(
              borderRadius: BorderRadius.circular(10),
              child: Container(
                  width: 100,
                  height: 45,
                  alignment: Alignment.center,
                  padding: EdgeInsets.symmetric(vertical: 10, horizontal: 20),
                  color: Colors.blue,
                  child: Text(
                    "$jian",
                    style: TextStyle(color: Colors.white),
                    textAlign: TextAlign.center,
                  )),
            ),

            GestureDetector(
              onTap: () {
                longPress();
              },
              child: Container(
                  alignment: Alignment.center,
                  width: 100,
                  height: 45,
                  decoration: BoxDecoration(
                      color: Colors.blue,
                      borderRadius: BorderRadius.circular(10)),
                  padding: EdgeInsets.symmetric(vertical: 10, horizontal: 20),
                  child: Text(
                    num.toString(),
                    style: TextStyle(color: Colors.white),
                    textAlign: TextAlign.center,
                  )),
            ),
            // CircleAvatar(
            //   radius: 60,
            //   backgroundImage: NetworkImage(
            //       "https://lmg.jj20.com/up/allimg/1113/062520105I8/200625105I8-6-1200.jpg"),
            // )
          ],
        ),

      ],
    ));
  }
}

class FeatureModel {
  String title;
  Widget child;

  FeatureModel(this.title, this.child);
}
